package blocos;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.*;

import blocos.Block;
import blocos.DiferentsBlocks;
import blocos.Persistence;

@SuppressWarnings("serial")
public abstract class BlockGUI extends JComponent implements Persistence {
	Block block;
	int x=0;
	int y=0;
	static int width=75;
	static int height=75;
	static int tolerance = 10;
	static int separation = 3;
	static int zOrder = 0;
	static String zoomSize = "75 px";
	public static double scale= 0.75;
	public static double prevScale = 1;
	private String slash;
	public static String defaultImagePathBig;
	public static String defaultImagePathMedium;
	public static String defaultImagePathSmall;
	Image imgBig;
	Image imgMedium;
	Image imgSmall;
	Image img;
	
	
   	public BlockGUI(){		
   		if(OSDetector.isWindows()) slash = "\\";
		else slash = "/";
   		defaultImagePathBig = System.getProperty("user.dir")+ slash+"images"+slash+"100 px"+slash;
   		defaultImagePathMedium = System.getProperty("user.dir")+ slash+"images"+slash+"75 px"+slash;
   		defaultImagePathSmall = System.getProperty("user.dir")+ slash+"images"+slash+"50 px"+slash;
   		repaint();
		revalidate() ;
		
	} 	
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		
		g.setColor(Color.BLACK);
        g.drawRect(x, y, width, height);
        g.setColor(Color.WHITE);
        g.fillRect(x+1, y+1, width-2, height-2);
        
        if(BlockGUI.scale == 1)
		{
			img = imgBig;
		}
		else if(BlockGUI.scale == 0.75)
		{
			img = imgMedium;
		}
		else
		{
			img = imgSmall;
		}
		g.drawImage(img,this.x+1 , this.y+1, this);

	}
	public void setPosition (int x, int y){
		this.x=x;
		this.y=y;
		this.repaint();
		this.revalidate();
	}
	
	public boolean isAttachedToBottomOf (BlockGUI b){
		if((Math.abs(this.x - b.x)<tolerance)&&(this.y - b.y<this.height+tolerance)&&(this.y - b.y>this.height)){
			return true;
		}
		else{
			return false;
		}
	}
	public boolean isAttachedToTopOf (BlockGUI b){
		if((Math.abs(this.x - b.x)<tolerance)&&(b.y - this.y<this.height+tolerance)&&(b.y - this.y>this.height)){
			return true;
		}
		else{
			return false;
		}
	}
	public boolean isAttachedToLeftOf (BlockGUI b){
		if((b.x - this.x<tolerance+this.width)&&(b.x - this.x>this.width)&&(Math.abs(this.y - b.y)<tolerance)){
			return true;
		}
		else{
			return false;
		}
	}
	
	public boolean isAttachedToRightOf(BlockGUI b){
		if((this.x - b.x<tolerance+this.width)&&(this.x - b.x>this.width)&&(Math.abs(this.y - b.y)<tolerance)){
			return true;
		}
		else{
			return false;
		}
	}
	public boolean containsBlock (BlockGUI b){
		if((this.contains(b.x, b.y)||
				(this.contains(b.x+b.width,b.y))||
				(this.contains(b.x+b.width,b.y+b.height))||
				(this.contains(b.x, b.y+b.height)))){
			return true;
		}
		else{
			return false;
		}
	}
	
	public void attachToRightOf (BlockGUI b){
		this.setPosition(b.x+b.width+separation, b.y);
		try {
			b.block.attachRight(this.block);
		} catch (DiferentsBlocks e) {
		}
	}
	public void attachToLeftOf (BlockGUI b){
		this.setPosition(b.x-b.width-separation, b.y);
		try {
			b.block.attachLeft(this.block);
		} catch (DiferentsBlocks e) {
		}
	}
	public void attachToTopOf (BlockGUI b){
		this.setPosition(b.x, b.y-b.height-separation);
		try {
			b.block.attachTop(this.block);
		} catch (DiferentsBlocks e) {
		}
	}
	public void attachToBottomOf (BlockGUI b){
		this.setPosition(b.x, b.y+b.height+separation);
		try {
			b.block.attachBottom(this.block);
		} catch (DiferentsBlocks e) {
		}
	}
	
	public void detachRight (){
		block.detachRight();
		
	}
	public void detachLeft (){
		block.detachLeft();
		
	}
	public void detachTop (){
		block.detachTop();
		
	}
	public void detachBottom (){
		block.detachBottom();
	}
	public void detachAll(){

		detachRight();
		detachLeft();
		detachTop();
		detachBottom();
	}
	public void remove(){
		this.setVisible(false);
	}
	public static void setZoomSize(int size)
	{
		
		if(size == 100)
		{
			zoomSize = "100 px";
			width = 100;
			height = 100;
			tolerance = 20;
			separation = 6;
			prevScale = scale;
			scale = 1;			
		}
		else if(size == 75)
		{
			zoomSize = "75 px";
			width = 75;
			height = 75;
			tolerance = 15;
			separation = 4;
			prevScale = scale;
			scale = 0.75;
		}
		else
		{
			zoomSize = "50 px";
			width = 50;
			height = 50;
			tolerance = 10;
			separation = 3;
			prevScale = scale;
			scale = 0.5;
		}
	}
	
	public void updatePosition()
	{
		this.setPosition((int)(x*scale/prevScale), (int)(y*scale/prevScale));
	}
}